<template>
  <div>
    <el-dialog :visible="showDialog" v-bind="$attrs" v-on="$listeners" @close="close"
      :title="form.id ? '修改目录' : '新增目录'">
      <el-form ref="formRef" :model="form" size="medium" label-width="100px">
        <el-form-item label="所属学科" prop="subjectName">
          <el-select v-model="form.subjectName" placeholder="请选择" clearable :style="{ width: '100%' }">
            <el-option v-for="item in subjectNameOptions" :key="item.id" :label="item.name" :value="item.label"
              :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="tagName">
          <el-input v-model="form.tagName" placeholder="请输入目录名称" clearable :style="{ width: '100%' }">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { update, add } from '@/api/hmmm/tags.js'
import { simple } from '@/api/hmmm/subjects.js'
export default {
  inheritAttrs: false,
  components: {},
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentId: {
      type: Number
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      form: {
        subjectName: undefined,
        tagName: undefined,
        subjectID: undefined
      },
      subjectNameOptions: []
    }
  },
  computed: {},
  watch: {},
  async created () {
    const res = await simple()
    console.log(res)
    this.subjectNameOptions = res.data
  },
  mounted () { },
  methods: {
    // onOpen () {},
    // onClose () {
    // },
    close () {
      // 手动重置数据
      this.form =
      {
        subjectName: undefined,
        tagName: undefined,
        subjectID: undefined
      }
      // this.$refs.formRef.resetFields()
      // 关闭弹窗
      this.$emit('update:showDialog', false)
    },
    handelConfirm (id) {
      // console.log('id', id)
      // subjectID = list.subjectID
      this.$refs.formRef.validate(async valid => {
        if (!valid) return
        if (this.form.id) {
          // 发送请求
          await update({
            subjectID: this.subjectNameOptions.find(el => el.label === this.form.subjectName).value,
            tagName: this.form.tagName,
            id: this.currentId
          })
        } else {
          // 新增
          await add({
            subjectID: this.subjectNameOptions.find(el => el.label === this.form.subjectName).value,
            tagName: this.form.tagName
          })
        }
        // 提示
        this.$message.success('操作成功')
        // 刷新列表
        this.$emit('success')
        // 关闭弹窗
        this.close()
      })
    }
  }
}

</script>
<style>

</style>
